﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using KOPLibrary.Controls;
using Microsoft.Office.Interop.Word;
using KOPLibrary.BarcodeLibrary;

namespace KOPLibrary.BarcodeManagement
{
    public static class BarcodeManager
    {
        public static Image GetBarcode(BarcodeLib.TYPE selectedtype, string text, bool includetext, Size? expectedsize)
        {
            BarcodeLib.Barcode brc = new BarcodeLib.Barcode();
            brc.IncludeLabel = includetext;
            if (expectedsize != null)
            {
                return brc.Encode(selectedtype, text, Color.Black, Color.White, expectedsize.Value.Width, expectedsize.Value.Height);
            }
            else
            {
                return brc.Encode(selectedtype, text, Color.Black, Color.White, 200, 100);
            }

        }

        public static List<KeyValuePair<string, Image>> GetBarcodes(BarcodeParameters settings, List<string> data)
        {
            List<KeyValuePair<string, Image>> resultimage = new List<KeyValuePair<string, Image>>();
            Size mysize = new Size(settings.Width, settings.Height);
            BarcodeLib.TYPE x = settings.BarcodeType;
            foreach (string item in data)
            {
                try
                {
                    resultimage.Add(new KeyValuePair<string, Image>(item, GetBarcode(x, item, settings.IncludeText, mysize)));
                }
                catch { continue; }
            }
            return resultimage;
        }
        public static void ShowBarcodesInDocument(List<KeyValuePair<string, Image>> image, Document doc)
        {
            if (image.Count > 0)
            {
                int rowcount = image.Count;               
                Range tablerange = doc.Range(0, 0);
                Table newtable = doc.Tables.Add(tablerange, rowcount, 2);
                KeyValuePair<string, Image> tempvalue;
                string clipboardtext = (System.Windows.Forms.Clipboard.ContainsText()) ? System.Windows.Forms.Clipboard.GetText() : null;
                for (int i = 1; i <= rowcount; i++)
                {
                    tempvalue = image[i - 1];
                    System.Windows.Forms.Clipboard.SetDataObject(tempvalue.Value, true);
                    newtable.Cell(i, 1).Range.Text = tempvalue.Key;
                    newtable.Cell(i, 2).Range.Paste();
                }
                newtable.Columns.AutoFit();
                newtable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                newtable.Borders.InsideLineWidth = WdLineWidth.wdLineWidth100pt;
                newtable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                newtable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth100pt;
                System.Windows.Forms.Clipboard.Clear();
                if (clipboardtext != null) System.Windows.Forms.Clipboard.SetText(clipboardtext);
            }
        
        }
    }
}
